var datewang = new Date();
var year = datewang.getFullYear();
var month = datewang.getMonth()+1;
var today = datewang.getDate();


// pages/appointment/details.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: year + "-" + month + "-" +today,
    time: "09:01"
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  onLoad: function(options) {
    var _that = this
    _that.setData({
      options: options
    })
    _that.details()
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

  //  建立档案请求接口
  details: function() {
    var that = this;
    var url = app.util.url('entry/wxapp/csd')
    app.util.request({
      url: url,
      data: {
        m: 'md_daojia',
        uid: wx.getStorageSync('userInfo')
      },
      success: function(res) {
        var list = res.data.data
        that.setData({
          List: list
        })
        console.log("建立档案=========>", res);

      }
    })
  },
  // 表单提交
  formDetails: function(t) {
    var that = this
    var date = this.data.date +"/"+ that.data.time //获去陪同时间的值
    var address = t.detail.value.address //获取陪同地址的值
    var contact = t.detail.value.contact //获取联系人的值
    var phone = t.detail.value.phone //获取手机号的值
    var _openid = wx.getStorageSync("openid");
    var url = app.util.url('entry/wxapp/accompantset')
    var urlpay = app.util.url('entry/wxapp/accstate')
    
    if (address == undefined || address == '') {
      wx.showToast({
        title: '请输入地址',
        icon: 'none'
      })
      return false
    } else if (contact == undefined || contact == '') {
      wx.showToast({
        title: '请输入联系人',
        icon: 'none'
      })
    } else if (phone == undefined || phone == "") {
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none'
      })
      return false
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      })
      return false
    } else {
      app.util.request({
        url: url,
        data: {
          m: 'md_daojia',
          uid: wx.getStorageSync('userInfo'),
          pid: that.data.options.pid,
          time: date,
          add: address,
          price: that.data.options.price,
          openid: _openid,
        },
        success: function(t) {
          console.log("确定预约=========>", t);
          wx.requestPayment({
            timeStamp: t.data.data.timeStamp,
            nonceStr: t.data.data.nonceStr,
            package: t.data.data.package,
            signType: t.data.data.signType,
            paySign: t.data.data.paySign,
            success: function (res) {
              console.log("这里是支付成功", res)
              app.util.request({
                url: urlpay,
                cachetime: "0",
                data: {
                  m: 'md_daojia',
                  orderid: t.data.data.id
                },
                success: function (t) {
                  console.log("改变订单状态", t)
                  wx.showToast({
                    title: "付款成功",
                    icon: "none",
                    image: "",
                    duration: 2e3,
                    mask: !0,
                    success: function (t) { },
                    fail: function (t) { },
                    complete: function (t) { }
                  }), setTimeout(function () {
                    wx.redirectTo({
                      url: '/pages/index/index'
                    })
                  }, 2e3);
                }
              });
            },
            fail: function (res) {
              wx.showToast({
                title: "支付失败",
                duration: 1e3
              });
            }
          });
          // that.setData({
          //   List: list
          // })

        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})